<template>
  <div style="width: 80%;margin: 0 auto">
    <br>
    <h3 style="text-align: center">个人中心</h3>
    <br>
    <el-form label-width="60px" :model="form" :rules="rules" ref="form">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <br>
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password" show-password></el-input>
      </el-form-item>
      <br>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="form.phone"></el-input>
      </el-form-item>
      <br>
      <el-form-item label="地址" prop="address">
        <el-input v-model="form.address" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item style="text-align: right">
        <el-button type="primary" @click="update()">修改</el-button>
        <el-button type="warning" @click="handleAddress()">编辑地址</el-button>
        <el-button type="danger" @click="logout">退出登录</el-button>
      </el-form-item>
    </el-form>
    <!--  修改地址  -->
    <el-dialog title="修改地址" :visible.sync="dialogVisible"  append-to-body>
      <el-row>
        <el-col :span="12">
          <el-form label-width="100px">
            <el-form-item label="地址" prop="address">
              <AreaData @change="test" width="100%"></AreaData>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取消</el-button>
        <el-button type="primary" @click="updateAddress()">修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getUserById, updateAddressById, updateUser} from "@/api/user";
import Cookies from "js-cookie";
import AreaData from "@/components/AreaData";
import router from "@/router";

export default {
  name: "Person",
  components: {AreaData},
  data() {
    return {
      form:{}, //表单列表
      dialogVisible: false,
      address: '',
      rules: {
        username: [
          {require: true, message: '请输入用户名', trigger: 'blur'},
          {min: 3, max: 8, message: '长度在3到8个字符', trigger: 'blur'}
        ],
        password: [
          {require: true, message: '请输入密码', trigger: 'blur'},
          {min: 3, max: 10, message: '长度在3到10位', trigger: 'blur'}
        ],
        phone: [
          {require: true, message: '请输入手机号', trigger: 'blur'},
          {min: 11, max: 11, message: '长度为11位', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    logout() {
      Cookies.remove('user')
      router.push({path: '/login'})
    },
    cancel() {
      this.dialogVisible = false
      this.$message.info('取消修改')
    },
    // 打开修改地址页面
    handleAddress() {
      this.dialogVisible = true
    },
    test(e) {
      this.address = e
    },
    //刷新页面
    getAll() {
      getUserById(this.form.user_id).then((res) => {
        this.form = res.data[0]
      })
    },
    // 通过user_id查看用户信息
    getData() {
      const user = Cookies.get('user')
      this.form = JSON.parse(user)
      getUserById(this.form.user_id).then((res) => {
        this.form = res.data[0]
      })
    },
    // 修改用户信息
    update() {
      this.$confirm('是否要修改用户信息','提示',{type: 'warning'}).then(() => {
        updateUser(this.form).then((data) => {
          if (data.code == 200) {
            this.$message.success(data.msg)
          } else {
            this.$message.error('修改失败')
          }
        }).finally(() => {
          this.getAll()
        })
      }).catch(() => {
        this.$message.info('取消修改')
      })
    },
    // 修改地址
    updateAddress() {
      console.log(this.address)
      if (this.address === '') {
        this.$message.info('请选择地址')
        return
      }
      let user_id;
      user_id = this.form.user_id
      let address;
      address = this.address
      const data = {
        user_id,
        address
      }
      updateAddressById(data).then((res) => {
        if (res.code === 200) {
          this.$message.success(res.msg)
          this.dialogVisible = false
        }else {
          this.$message.error('修改失败')
        }
      }).finally(() => {
        this.getAll()
      })
    }
  },
  mounted() {
    this.getData()
  }
}
</script>

<style scoped>

</style>
